package cn.sym.basic.controller;

import cn.sym.basic.dto.LoginDto;
import cn.sym.basic.dto.WechatBinderDto;
import cn.sym.basic.exception.MyException;
import cn.sym.basic.service.ILoginService;
import cn.sym.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService iLoginService;

    /**
     * 账号密码登录  type  0表示employee   1表示admin
     * @param loginDto
     * @return
     */
    @RequestMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        System.out.println(loginDto);
        try {
            Map<String, Object> account = iLoginService.account(loginDto);
            System.out.println("返回的信息："+account);
            return  AjaxResult.my().setResultObj(account);
        }catch (MyException e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("请稍后再试！");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }

    }
    /**
     * 微信登录接口
     * @param param 中code 表示用户扫码之后，微信返回的授权码
     * @return
     */
    @RequestMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String,String> param){
        try {
            return iLoginService.wechat(param);
        }catch (MyException e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("请稍后再试！");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }

    }
    /**
     * 微信绑定接口
     * @param
     * @return
     */
    @RequestMapping("/wechatBinder")
    public AjaxResult wechatBinder(@RequestBody WechatBinderDto wechatBinderDto){
        try {
            Map<String, Object> map = iLoginService.wechatBinder(wechatBinderDto);
            return AjaxResult.my().setResultObj(map);
        }catch (MyException e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("请稍后再试！");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }

    }
}
